package wowmobileclient;

import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.TiledLayer;

public class FightView {

    public Graphics g;
    public int width;
    public int height;
    public int blocksize;
    public Vector players;
    public Vector enemies;
    private Image tiled;
    private TiledLayer layer;
    private int[] layer_size = new int[2];
    private int all_time = 100;
    private int time = 20;
    public FightHud fh;

    public FightView(Graphics g, int width, int height, int blocksize) throws IOException {
        this.g = g;
        this.width = width;
        this.height = height;
        fh= new FightHud(g,width,height);
        this.layer_size[0] = 10;
        this.layer_size[1] = 5;
        this.blocksize = blocksize;

        tiled = Image.createImage("/tiled.png");
        layer = new TiledLayer(this.layer_size[0] * this.blocksize, this.layer_size[1] * this.blocksize, tiled, this.blocksize, this.blocksize);
    }

    public void draw(int enemies, int allies) {
        this.cleanLayer();
        this.drawPlayers(false, allies);
        this.drawPlayers(true, enemies);

        g.setColor(0, 0, 0);
        g.fillRect(0, 0, width, height);
        layer.setPosition((this.width - this.layer_size[0] * this.blocksize) / 2, this.blocksize);
        layer.paint(g);

        //texts
        g.setFont(Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL));
        g.setColor(128, 128, 128);
        g.drawString("Enemies:", 70, 5, Graphics.TOP | Graphics.HCENTER);

        g.setColor(128, 128, 128);
        g.drawString("Players:", width - 70, 5, Graphics.TOP | Graphics.HCENTER);


        //draw time
        g.setColor(0, 0, 255);
        g.fillRect(0, this.layer_size[1] * this.blocksize + 20, width, 15);

        g.setColor(255, 0, 0);
        double factor = (double) this.time / (double) this.all_time;
        g.fillRect((int) factor * width, this.layer_size[1] * this.blocksize + 20, (int) (factor * width), 15);
    }

    private void drawPlayers(boolean enemies, int howMany) {
        int x = 3;
        int y = 1;
        Vector list = this.players;
        if (enemies) {
            x = this.layer_size[0] - 4;
            list = this.enemies;
        }

        //list.size();
        for (int i = 0; i < 6; ++i) {
            if (i % 3 == 0 && i != 0) {
                if (enemies) {
                    x = x + 2;
                    y = 1;
                } else {
                    x = x - 2;
                    y = 1;
                }
            }
            if(i<howMany)
                layer.setCell(x, y, 1);
            else
                layer.setCell(x,y,7);

            ++y;
            if (i == 5) {
                break;
            }
        }
    }

    private void cleanLayer() {
        for (int i = 0; i < this.layer_size[0]; ++i) {
            for (int j = 0; j < this.layer_size[1]; ++j) {
                layer.setCell(i, j, 7);
            }
        }
    }

    public void setTime(int time, int all_time) {
        this.time = time;
        this.all_time = all_time;
    }
}
